package gnss

import (
	"fmt"
)

type RtcmRawSatSigObs struct {
	NaviSys  byte
	SatId    byte
	RoughRR  float64
	RoughPRR int16 // msm7

	SigId   byte
	LktV    uint16
	PsRng0  float64
	PhRng0  float64
	HalfAmb byte
	Cnr     float32

	PhaRR float64 // msm4 GNSS signal fine PhaseRangeRates DF404 int15 (Ncell times) 15*Ncell
}

func (this *RtcmRawSatSigObs) String() string {
	return fmt.Sprintf("%s %s C %s L %s D %s S %s", this.GetPrnCode(), GetSignCode(this.SigId, this.NaviSys),
		RinexFormatFloat(this.GetPsRng()), RinexFormatFloat(this.GetPhRng()), RinexFormatFloat(this.GetDoppler()), RinexFormatFloat(float64(this.Cnr)))
}

func (this *RtcmRawSatSigObs) StringWithRaw() string {
	return fmt.Sprintf("%s %.4f %s C %s(%.4f)  L %s(%.4f) D %s S %s", this.GetPrnCode(), this.RoughRR, GetSignCode(this.SigId, this.NaviSys),
		RinexFormatFloat(this.GetPsRng()), this.PsRng0, RinexFormatFloat(this.GetPhRng()), this.PhRng0, RinexFormatFloat(this.GetDoppler()), RinexFormatFloat(float64(this.Cnr)))
}

func (this *RtcmRawSatSigObs) GetPsRng() float64 {
	if this.PsRng0 == PS_INVALID_VALUE {
		return 0
	}
	return this.PsRng0 + this.RoughRR
}

/*
*
锁定时间 毫秒
*/
func (this *RtcmRawSatSigObs) GetLockTime() int64 {
	if this.LktV > 15 {
		return LockIndicator2MsMsm7(this.LktV)
	}
	return LockIndicator2Ms(uint8(this.LktV))
}

func (this *RtcmRawSatSigObs) GetPrnCode() string {
	return GetPrnStrCode(this.SatId, this.NaviSys)
}

func (this *RtcmRawSatSigObs) WaveLength() float64 {
	return SigWaveLen(this.NaviSys, this.SigId, this.SatId)
}

func (this *RtcmRawSatSigObs) GetPhRng() float64 {
	if this.PhRng0 == PH_INVALID_VALUE {
		return 0
	}

	wl := this.WaveLength()
	if wl == 0 {
		return 0
		//panic(fmt.Sprintf("未知的波长[%s][%d]", this.GetPrnCode(), this.SigId))
	}
	return (this.PhRng0 + this.RoughRR) / wl
}

func (this *RtcmRawSatSigObs) GetDoppler() float64 {
	if this.PhaRR == PS_INVALID_VALUE {
		return 0
	}
	//if this.RoughPRR == 0 {
	//	return 0
	//}
	//wl := this.WaveLength()
	//if wl == 0 {
	//	return 0
	//	//panic(fmt.Sprintf("未知的波长[%s][%d]", this.GetPrnCode(), this.SigId))
	//}
	// return (float64(this.RoughPRR) + this.PhaRR) / wl

	rval := float64(float64(this.RoughPRR) + float64(this.PhaRR)*1e-4)
	return rval
}

func (this *RtcmRawSatSigObs) GetPhRR() float64 {
	if this.PhaRR == PS_INVALID_VALUE {
		return 0
	}
	rval := float64(float64(this.RoughPRR) + float64(this.PhaRR)*1e-4)
	return rval
}
